package com.ysj.demo.accompanynetwork.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ysj.demo.accompanynetwork.R;
import com.ysj.demo.accompanynetwork.util.StatusBarUtil;
import com.ysj.demo.accompanynetwork.adapter.FourFragmentAdapter;
import com.ysj.demo.accompanynetwork.fragment.FindPlayerFragment;
import com.ysj.demo.accompanynetwork.fragment.HomeFragment;
import com.ysj.demo.accompanynetwork.fragment.RentYourselfFragment;
import com.ysj.demo.accompanynetwork.fragment.UserFragment;

import java.util.ArrayList;

public class MainActivity extends FragmentActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    private ArrayList<TextView> tv_menus;
    private ArrayList<ImageView> imgv_menus;
    private ViewPager mViewPager;
    private ArrayList<Fragment> mFragments;
    private FourFragmentAdapter mMainMenuAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StatusBarUtil.fullScreen(this);

        initView();
        initData();
        initListener();
    }

    private void initView() {
        mViewPager = findViewById(R.id.main_view_pager);
        tv_menus = new ArrayList<>();
        tv_menus.add((TextView) findViewById(R.id.main_home_tv));
        tv_menus
                .add((TextView) findViewById(R.id.main_player_tv));
        tv_menus.add((TextView) findViewById(R.id.main_rent_tv));
        tv_menus.add((TextView) findViewById(R.id.main_me_tv));
        imgv_menus = new ArrayList<>();
        imgv_menus.add((ImageView) findViewById(R.id.main_home_iv));
        imgv_menus
                .add((ImageView) findViewById(R.id.main_player_iv));
        imgv_menus
                .add((ImageView) findViewById(R.id.main_rent_iv));
        imgv_menus.add((ImageView) findViewById(R.id.main_me_iv));
    }

    private void initData() {
        mFragments = new ArrayList<>();
        mFragments.add(new HomeFragment());
        mFragments.add(new FindPlayerFragment());
        mFragments.add(new RentYourselfFragment());
        mFragments.add(new UserFragment());
        mMainMenuAdapter = new FourFragmentAdapter(getSupportFragmentManager(),
                mFragments);
        setMenuSelector(0); // 默认选中第一个菜单项“微信”
    }

    /**
     * 选中指定的菜单项并显示对应的Fragment
     *
     * @param index
     */
    private void setMenuSelector(int index) {
        reSetSelected();
        tv_menus.get(index).setSelected(true);
        //    imgv_menus.get(index).setSelected(true);
        mViewPager.setCurrentItem(index);
    }

    /**
     * 重置底部菜单所有ImageView和TextView为未选中状态
     */
    private void reSetSelected() {
        for (int i = 0; i < tv_menus.size(); i++) {
            tv_menus.get(i).setSelected(false);
            //       imgv_menus.get(i).setSelected(false);
        }
    }

    private void initListener() {
        mViewPager.setAdapter(mMainMenuAdapter);
        mViewPager.addOnPageChangeListener(this);
        findViewById(R.id.main_home_ll).setOnClickListener(this);
        findViewById(R.id.main_player_ll).setOnClickListener(this);
        findViewById(R.id.main_rent_ll).setOnClickListener(this);
        findViewById(R.id.main_me_ll).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_home_ll:
                setMenuSelector(0);
                break;
            case R.id.main_player_ll:
                setMenuSelector(1);
                break;
            case R.id.main_rent_ll:
                setMenuSelector(2);
                break;
            case R.id.main_me_ll:
                setMenuSelector(3);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @Override
    public void onPageSelected(int arg0) {
        setMenuSelector(arg0);
    }
}
